package com.kungfuhacking.wristbandpro.my.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kungfuhacking.wristbandpro.R;
import com.kungfuhacking.wristbandpro.base.view.BaseActivity;
import com.kungfuhacking.wristbandpro.my.bean.OldDetail;

import static com.kungfuhacking.wristbandpro.R.id.cb_femal;
import static com.kungfuhacking.wristbandpro.R.id.cb_mal;

/**
 * 修改老人性别
 * Created by Aaron on 17/6/1.
 */
public class ModifyOldSexActivity extends BaseActivity implements View.OnClickListener {

    private ImageView cbfemal;
    private ImageView cbmal;
    private OldDetail oldDetail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_modify_sex);
        initWidget();

        oldDetail = getIntent().getParcelableExtra("oldInfo");

        if ("女".equals(oldDetail.getData().getSex())) {
            cbfemal.setSelected(true);
            cbmal.setSelected(false);
        } else {
            cbmal.setSelected(true);
            cbfemal.setSelected(false);
        }


    }

    private void initWidget() {
        RelativeLayout rlfemal = (RelativeLayout) findViewById(R.id.rl_femal);
        cbfemal = (ImageView) findViewById(cb_femal);
//        TextView tvfemale = (TextView) findViewById(R.id.tv_female);
        RelativeLayout rlmeal = (RelativeLayout) findViewById(R.id.rl_meal);
        cbmal = (ImageView) findViewById(cb_mal);
//        TextView tvmale = (TextView) findViewById(R.id.tv_male);
        TextView tvright = (TextView) findViewById(R.id.tv_right);
//        TextView tvtitle = (TextView) findViewById(R.id.tv_title);
        ImageView ivleft = (ImageView) findViewById(R.id.iv_left);

        rlfemal.setOnClickListener(this);
        rlmeal.setOnClickListener(this);
        ivleft.setOnClickListener(this);
        tvright.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.rl_femal://女选中男不选中
                cbmal.setSelected(false);
                cbfemal.setSelected(true);
                break;

            case R.id.rl_meal://男选中女不选中
                cbmal.setSelected(true);
                cbfemal.setSelected(false);
                break;

            case R.id.iv_left:
                finish();
                break;

            case R.id.tv_right:

                if ("女".equals(oldDetail.getData().getSex())) {
                    //修改前 是 女
                    if (cbmal.isSelected()) {
                        //修改了
                        updateSex();
                    }

                } else {
                    if (cbfemal.isSelected()) {
                        //修改了
                        updateSex();
                    }
                }

                finish();

                break;

        }
    }

    private void updateSex() {

        Intent i = new Intent();
        i.putExtra("sex", cbmal.isSelected() ? "男" : "女");
        setResult(0x102, i);
        finish();
    }
}
